<?php
require_once "common.php";

class Login {
    public static function signIn($name,$password) {
        if (!isset($name) || !isset($password)) { return; }
        $pwdMd5 = md5($password);
        $employee = User::first(array('name' => $name,'password' => $pwdMd5 ));
        if ($employee != null) {
            $_SESSION['user'] = $employee->id;
            return $employee;
        }
    }

    public static function signOut() {
        session_unset();
        session_destroy();
    }
    
    public static function getLoggedIn() {
        if (isset($_SESSION['user'])) {
            try {
                return User::find($_SESSION['user']);
            } catch (ActiveRecord\RecordNotFound $ex) {
                error_die("user deleted");
            }
        }
    }
}
?>
